home *** CD-ROM | disk | FTP | other *** search
- Path: dawn.mmm.com!news
- From: kjhopps@mmm.com (Kevin J Hopps)
- Newsgroups: comp.lang.c++
- Subject: Re: void pointers
- Date: 22 Jan 1996 13:47:47 GMT
- Organization: 3M - St. Paul, MN 55144-1000 US
- Message-ID: <4e04i3$ete@dawn.mmm.com>
- References: <Pine.SUN.3.91.960105113409.18158A-100000@phoenix.acms.arizona.edu> <DKx5H9.1rF@news.zippo.com>
- Reply-To: kjhopps@mmm.com
- X-Newsreader: TIN [version 1.2 PL2]
-
- Jim McFarland (jgm6@orkand.em.cdc.gov) wrote:
- > Kenton White <jwhite@phoenix.acms.arizona.edu> wrote:
- > >
- > >
- > >I am trying to create a class that stores pointers to other classes in an
- > >array of void pointers. Since void pointers cannot be dereferenced, is
- > >there any way to cast the void pointer to a non-void pointer and then
- > >dereference?
- > >
- > You are trying to use a very C-oriented solution, instead of taking
- > advantage of the power of object-oriented programming offered by C++.
- > What you are trying to create is a container class. Your compiler may
- > already provide a class that will do this. If not, you should use
- > polymorphism to create a container that can contain pointers to various
- > classes, without needing to use void pointers. To do this, your classes
- > will need to have a common base class.
-
- When creating a template container class it might be good to encapsulate
- behavior common to all template instantiations into a single base class
- that operates on void pointers. This helps alleviate some of the code-
- bloat that naive use of templates can cause. In cases like these, using
- void pointers may be advantageous.
-
- Yes, it is quite possible to cast void pointers to non-void pointers.
- In the old syntax, for example:
- T* Container<T>::next()
- {
- void* item = Base::next();
- return (T*)item;
- }
- I believe the new syntax would be:
- return reinterpret_cast<T*>(item);
-
- OTOH, it's hard to justify inventing container classes these days,
- except perhaps for educational purposes, since there are so many class
- libraries around.
- --
- Kevin J. Hopps e-mail: kjhopps@mmm.com
- 3M Company phone: (612) 737-4643
- 3M Center, Bldg. 235-2D-57 fax: (612) 737-2700
- St. Paul, MN 55144-1000 Opinions are my own. I don't speak for 3M.
- But 3M speaks for me -- I did not write the following line:
-
- Opinions expressed herein are my own and may not represent those of 3M.
-